/*
 * GContestHistoryRecord.java
 * Created on 30 janv. 2008
 *
 * Glacéo Internet Platform
 * http://sourceforge.net/projects/chm/
 *
 * Copyright (c) 2005-2008, Jens and Jan Janke (VirtualHockey Project)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.hockeo.vhbase.data.dao.contest;


import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import com.hockeo.vhbase.data.dao.IGEntity;
import com.hockeo.vhbase.data.dao.club.GClub;
import com.hockeo.vhbase.data.dao.season.GSeason;
import com.hockeo.vhbase.data.helper.GContestHistoryType;

/**
 * Represents a record in the contest history. For each contest and season, one contest
 * history entry is created in order to record the championship/cup winner and the
 * second-best club.
 *
 * @version $Id$
 * @author jjanke
 */
@Entity
@Table( name = "h_contest_history" )
public class GContestHistoryRecord implements IGEntity
{
  @Id
  @Column( name = "id" )
  @GeneratedValue( strategy = GenerationType.AUTO )
  private int                 d_nId;

  @ManyToOne(fetch = FetchType.LAZY )
  @JoinColumn( name = "season_id" )
  private GSeason             d_season;

  @ManyToOne(fetch = FetchType.LAZY )
  @JoinColumn( name = "contest_id" )
  private GContest            d_contest;

  @ManyToOne(fetch = FetchType.LAZY )
  @JoinColumn( name = "club_1st_id" )
  private GClub               d_clubWinner;

  @ManyToOne(fetch = FetchType.LAZY )
  @JoinColumn( name = "club_2nd_id" )
  private GClub               d_clubSecond;

  @Enumerated( EnumType.STRING )
  @Column( name = "type" )
  private GContestHistoryType d_type;

  /** Default parameterless constructor. */
  protected GContestHistoryRecord()
  {}

  /**
   * Creates a new GContestHistoryRecord.
   *
   * @param type the contest history type
   * @param season the season for which to create the entry
   * @param contest the contest
   * @param clubWinner the winning club
   * @param clubSecond the second of the competition
   */
  public GContestHistoryRecord( GContestHistoryType type, GSeason season, GContest contest, GClub clubWinner,
                                GClub clubSecond )
  {
    d_type = type;
    d_season = season;
    d_contest = contest;
    d_clubWinner = clubWinner;
    d_clubSecond = clubSecond;
  }

  public GSeason getSeason()
  {
    return d_season;
  }

  public GContest getContest()
  {
    return d_contest;
  }

  public GClub getClubWinner()
  {
    return d_clubWinner;
  }

  public GClub getClubSecond()
  {
    return d_clubSecond;
  }

  public GContestHistoryType getType()
  {
    return d_type;
  }

  public int getId()
  {
    return d_nId;
  }

  public Class<? extends IGEntity> getGEntityClass()
  {
    return GContestHistoryRecord.class;
  }

  public boolean instanceOf( Class<? extends IGEntity> classOther )
  {
    return classOther.isAssignableFrom( getGEntityClass() );
  }

  /*
   * @see java.lang.Object#equals(java.lang.Object)
   */
  @Override
  public boolean equals( Object obj )
  {
    try
    {
      return d_nId == ( (GContestHistoryRecord) obj ).getId();
    }
    catch ( Exception ex ) // especially ClassCastException(s) may happen
    {
      return false;
    }
  }

  /*
   * @see java.lang.Object#hashCode()
   */
  @Override
  public int hashCode()
  {
    return d_nId;
  }

  /*
   * @see java.lang.Object#toString()
   */
  @Override
  public String toString()
  {
    return "Contest history for season " + d_season.getName() + " and contest " + d_contest.getId() + " - Champion: "
        + d_clubWinner.getId();
  }
}
